home *** CD-ROM | disk | FTP | other *** search
/ Programming Languages Suite / ProgramD2.iso / Borland / Borland C++ V5.02 / OWLINC.PAK / CONTAIN.H < prev    next >
C/C++ Source or Header  |  1997-05-06  |  9KB  |  379 lines

  1. //----------------------------------------------------------------------------
  2. // ObjectWindows
  3. // Copyright (c) 1993, 1997 by Borland International, All Rights Reserved
  4. //
  5. //$Revision:   10.3  $
  6. //
  7. // Definition of container classes used and made available by OWL
  8. //----------------------------------------------------------------------------
  9. #if !defined(OWL_CONTAIN_H)
  10. #define OWL_CONTAIN_H
  11.  
  12. #if !defined(OWL_DEFS_H)
  13. # include <owl/defs.h>
  14. #endif
  15. #if !defined(CLASSLIB_ARRAYS_H)
  16. # include <classlib/arrays.h>
  17. #endif
  18. #include <limits.h>
  19.  
  20. #if defined(BI_NAMESPACE)
  21. namespace OWL {
  22. #endif
  23.  
  24. // Generic definitions/compiler options (eg. alignment) preceeding the 
  25. // definition of classes
  26. #include <services/preclass.h>
  27.  
  28. //
  29. // class TStringArray
  30. // ~~~~~ ~~~~~~~~~~~~
  31. class _OWLCLASS TStringArray {
  32.   public:
  33.     typedef void (*IterFunc)(string&, void*);
  34.     typedef int  (*CondFunc)(const string&, void*);
  35.  
  36.     TStringArray(int upper, int lower, int delta);
  37.     int LowerBound() const;
  38.     int UpperBound() const;
  39.     uint ArraySize() const;
  40.     int IsFull() const;
  41.     int IsEmpty() const;
  42.     uint GetItemsInContainer() const;
  43.     int Add(const string& t);
  44.     int Detach(const string& t);
  45.     int Detach(int loc);
  46.     int Destroy(const string& t);
  47.     int Destroy(int loc);
  48.     int HasMember(const string& t) const;
  49.     int Find(const string& t) const;
  50.     string& operator [](int loc);
  51.     string& operator [](int loc) const;
  52.     void ForEach(IterFunc iter, void* args);
  53.     string* FirstThat(CondFunc cond, void* args) const;
  54.     string* LastThat(CondFunc cond, void* args) const;
  55.     void Flush();
  56.  
  57.   private:
  58.     TArrayAsVector<string> Data;
  59.  
  60.   friend class TStringArrayIterator;
  61. };
  62.  
  63. //
  64. // class TStringArrayIterator
  65. // ~~~~~ ~~~~~~~~~~~~~~~~~~~~
  66. class TStringArrayIterator : public TArrayAsVectorIterator<string> {
  67.   public:
  68.     TStringArrayIterator(const TStringArray& array);
  69. };
  70.  
  71. //
  72. // class TInt
  73. // ~~~~~ ~~~~
  74. // Wrapper for an int to allow it to be contained by a BIDS container. This
  75. // is to avoid collision with an function overloaded on both int and T
  76. //
  77. class TInt {
  78.   public:
  79.     TInt() {}
  80.     TInt(int _i) {i = _i;}
  81.     operator int() const {return i;}
  82.  
  83.   private:
  84.     int i;
  85. };
  86.  
  87. //
  88. // class TIntArray
  89. // ~~~~~ ~~~~~~~~~
  90. class _OWLCLASS TIntArray {
  91.   public:
  92.     typedef void (*IterFunc)(TInt&, void*);
  93.     typedef int  (*CondFunc)(const TInt&, void*);
  94.  
  95.     TIntArray(int upper, int lower, int delta);
  96.     int LowerBound() const;
  97.     int UpperBound() const;
  98.     uint ArraySize() const;
  99.     int IsFull() const;
  100.     int IsEmpty() const;
  101.     uint GetItemsInContainer() const;
  102.     int Add(const TInt& t);
  103.     int Detach(const TInt& t);
  104.     int Detach(int loc);
  105.     int Destroy(const TInt& t);
  106.     int Destroy(int loc);
  107.     int HasMember(const TInt& t) const;
  108.     int Find(const TInt& t) const;
  109.     TInt& operator [](int loc);
  110.     TInt& operator [](int loc) const;
  111.     void ForEach(IterFunc iter, void* args);
  112.     TInt* FirstThat(CondFunc cond, void* args) const;
  113.     TInt* LastThat(CondFunc cond, void* args) const;
  114.     void Flush();
  115.  
  116.   private:
  117.     TArrayAsVector<TInt> Data;
  118.  
  119.   friend class TIntArrayIterator;
  120. };
  121.  
  122. //
  123. // class TIntArrayIterator
  124. // ~~~~~ ~~~~~~~~~~~~~~~~~
  125. class TIntArrayIterator : public TArrayAsVectorIterator<TInt> {
  126.   public:
  127.     TIntArrayIterator(const TIntArray& array);
  128. };
  129.  
  130. //
  131. // class TUint32Array
  132. // ~~~~~ ~~~~~~~~~~~~
  133. class _OWLCLASS TUint32Array {
  134.   public:
  135.     typedef void (*IterFunc)(uint32&, void*);
  136.     typedef int  (*CondFunc)(const uint32&, void*);
  137.  
  138.     TUint32Array(int upper, int lower, int delta);
  139.  
  140.     int LowerBound() const;
  141.     int UpperBound() const;
  142.     uint ArraySize() const;
  143.     int IsFull() const;
  144.     int IsEmpty() const;
  145.     uint GetItemsInContainer() const;
  146.     int Add(const uint32& t);
  147.     int Detach(const uint32& t);
  148.     int Detach(int loc);
  149.     int Destroy(const uint32& t);
  150.     int Destroy(int loc);
  151.     int HasMember(const uint32& t) const;
  152.     int Find(const uint32& t) const;
  153.     uint32& operator [](int loc);
  154.     uint32& operator [](int loc) const;
  155.     void ForEach(IterFunc iter, void* args);
  156.     uint32* FirstThat(CondFunc cond, void* args) const;
  157.     uint32* LastThat(CondFunc cond, void* args) const;
  158.     void Flush();
  159.  
  160.   private:
  161.     TArrayAsVector<uint32> Data;
  162.  
  163.   friend class TUint32ArrayIterator;
  164. };
  165.  
  166. //
  167. // class TUint32ArrayIterator
  168. // ~~~~~ ~~~~~~~~~~~~~~~~~~~~
  169. class TUint32ArrayIterator : public TArrayAsVectorIterator<uint32> {
  170.   public:
  171.     TUint32ArrayIterator(const TUint32Array& array);
  172. };
  173.  
  174. //
  175. // Old names provided for compatability
  176. //
  177. typedef TUint32Array TDwordArray;
  178. typedef TUint32ArrayIterator TDwordArrayIterator;
  179.  
  180.  
  181. //----------------------------------------------------------------------------
  182.  
  183. //
  184. // Maximum number of entries in each Vector
  185. //
  186. const uint MAXENTRIES = uint((ulong(UINT_MAX) - sizeof(uint))/
  187.                                      sizeof(void*));
  188. //
  189. // class TCollection
  190. // ~~~~~ ~~~~~~~~~~~
  191. // Simple template holding pointers to Ts used internally by
  192. // ObjectWindows Controls
  193. //
  194. template <class T> class TCollection {
  195.   public:
  196.     TCollection(uint aLimit, uint aDelta, bool shldDel = true);
  197.    ~TCollection();
  198.  
  199.     T&          operator [](uint index) const;
  200.     int         GetCount() const;
  201.  
  202.     int         Append(T* item);
  203.     void        InsertAt(uint index, T* item);
  204.     void        RemoveAt(uint index);
  205.     void        RemoveAll();
  206.     void        FreeAt(uint index);
  207.     void        FreeAll();
  208.  
  209.   protected:
  210.     void        SetLimit(uint aLimit);
  211.  
  212.   private:
  213.     TCollection(const TCollection&);
  214.     TCollection& operator =(const TCollection&);
  215.  
  216.     void        FreeItem(T* item);
  217.     T**         items;              // Pointer to array of T*
  218.     uint        count;              // Number of items
  219.     uint        limit;              // Size array is allocated for
  220.     uint        delta;              // Inc. delta for resizing
  221.     bool        shouldDelete;       // Should free in destructor
  222. };
  223.  
  224. // Generic definitions/compiler options (eg. alignment) following the 
  225. // definition of classes
  226. #include <services/posclass.h>
  227.  
  228. #if defined(BI_NAMESPACE)
  229. } // namespace OWL
  230. #endif
  231.  
  232. //----------------------------------------------------------------------------
  233. // Inline implementations
  234. //
  235.  
  236. //
  237. inline TStringArrayIterator::TStringArrayIterator(const TStringArray& array)
  238. :
  239.   TArrayAsVectorIterator<string>(array.Data)
  240. {
  241. }
  242.  
  243. //
  244. inline TIntArrayIterator::TIntArrayIterator(const TIntArray& array)
  245. :
  246.   TArrayAsVectorIterator<TInt>(array.Data)
  247. {
  248. }
  249.  
  250. //
  251. inline TUint32ArrayIterator::TUint32ArrayIterator(const TUint32Array& array)
  252. :
  253.   TArrayAsVectorIterator<uint32>(array.Data)
  254. {
  255. }
  256.  
  257. //
  258. template<class T>
  259. TCollection<T>::TCollection(uint aLimit, uint aDelta, bool shldDel)
  260. : count(0), items(0), limit(0), delta(aDelta), shouldDelete(shldDel)
  261. {
  262.   SetLimit(aLimit);
  263. }
  264.  
  265. //
  266. template<class T>
  267. TCollection<T>::~TCollection()
  268. {
  269.   if (shouldDelete)
  270.     FreeAll();
  271.   SetLimit(0);
  272. }
  273.  
  274. //
  275. template<class T>
  276. int TCollection<T>::GetCount() const
  277. {
  278.   return count;
  279. }
  280.  
  281. //
  282. template<class T>
  283. void TCollection<T>::RemoveAll()
  284. {
  285.   count = 0;
  286. }
  287.  
  288. //
  289. template<class T>
  290. T& TCollection<T>::operator[](uint index) const
  291. {
  292.   PRECONDITION(index < count);
  293.   return *items[index];
  294. }
  295.  
  296. //
  297. template<class T>
  298. int TCollection<T>::Append(T* item)
  299. {
  300.   InsertAt(count, item);
  301.   return count-1;
  302. }
  303.  
  304. //
  305. template<class T>
  306. void TCollection<T>::InsertAt(uint index, T* item)
  307. {
  308.   PRECONDITION(index <= count);
  309.   if (count == limit)
  310.     SetLimit(count + delta);
  311.   if (count-index != 0)
  312.     memmove((void*)&items[index+1], (void*)&items[index],
  313.             (count-index)*sizeof(T*));
  314.   items[index] = item;
  315.   count++;
  316. }
  317.  
  318. //
  319. template<class T>
  320. void TCollection<T>::RemoveAt(uint index)
  321. {
  322.   PRECONDITION(index < count);
  323.   if (index != count-1)
  324.     memmove((void*)&items[index], (void*)&items[index+1],
  325.             (count-(index+1))*sizeof(T*));
  326.   count--;
  327. }
  328.  
  329. //
  330. template<class T>
  331. void TCollection<T>::FreeAt(uint index)
  332. {
  333.   T& item = operator[](index);
  334.   RemoveAt(index);
  335.   FreeItem(&item);
  336. }
  337.  
  338. //
  339. template<class T>
  340. void TCollection<T>::FreeAll()
  341. {
  342.   for (uint i = 0; i < count; i++)
  343.     FreeItem(&(operator[](i)));
  344.   count = 0;
  345. }
  346.  
  347. //
  348. template<class T>
  349. void TCollection<T>::FreeItem(T* item)
  350. {
  351.   delete item;
  352. }
  353.  
  354. //
  355. template<class T>
  356. void TCollection<T>::SetLimit(uint aLimit)
  357. {
  358.   if (aLimit < count)
  359.     aLimit = count;
  360.   CHECK(aLimit <= MAXENTRIES);
  361.  
  362.   if (aLimit != limit) {
  363.     T **aItems;
  364.  
  365.     if (aLimit == 0)
  366.       aItems = 0;
  367.     else {
  368.       aItems = new T *[aLimit];
  369.       if (count != 0)
  370.         memcpy(aItems, items, count*sizeof(T*));
  371.     }
  372.     delete [] items;
  373.     items = aItems;
  374.     limit = aLimit;
  375.   }
  376. }
  377.  
  378. #endif  // OWL_CONTAIN_H
  379.